﻿Imports MindFusion.Diagramming
Imports MindFusion.Diagramming.WinForms

Public Class UcnNodesToolbar
    'represents drag&droppable toolbar

    Private MsPathToDiagram As String = ""

#Region "Properties"
#Region "Public properties"
    Public Property PathToDiagram() As String
        Get
            Return MsPathToDiagram
        End Get
        Set(ByVal IsValue As String)
            MsPathToDiagram = IsValue
        End Set
    End Property

    Public ReadOnly Property DiagramView() As DiagramView
        Get
            Return fcDiagramView
        End Get
    End Property

    Public ReadOnly Property Diagram() As Diagram
        Get
            Return fcDiagram
        End Get
    End Property
#End Region
#End Region
#Region "Constructors"
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub
#End Region
#Region "Events handler"
    'selecting new item and starting dragging item
    Private Sub ilbNodes_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ilbNodes.MouseDown
        'accept only left mouse button
        Dim LiItemUnderCursor As Integer = ilbNodes.IndexFromPoint(New Point(e.X, e.Y))
        Select Case Control.MouseButtons
            Case Windows.Forms.MouseButtons.Left
                If LiItemUnderCursor > -1 Then
                    'select actual item
                    ilbNodes.SelectedIndex = LiItemUnderCursor
                    'starting draging an item
                    Dim LeTypeOfDragDropItem As GeTypeOfDragDropItem = GeTypeOfDragDropItem.kkTemplateTable
                    DoDragDrop(New ClsDragDropItem(LeTypeOfDragDropItem), DragDropEffects.Copy)
                End If
            Case Windows.Forms.MouseButtons.Right
                If LiItemUnderCursor > -1 Then
                    'select actual item
                    ilbNodes.SelectedIndex = LiItemUnderCursor
                    xtrPopupMenu.ShowPopup(PointToScreen(e.Location))
                End If
        End Select
    End Sub
    'removing template
    Private Sub barbtnRemoveTemplate_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnRemoveTemplate.ItemClick
        RemoveTemplateFromToolbar()
    End Sub
    'renaming template
    Private Sub barBtnRenameTemplate_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnRenameTemplate.ItemClick
        Dim LsNewName As String = ""
        LsNewName = InputBox("Zadajte novy nazov sablony", "Premenovanie sablony")
        If LsNewName <> "" Then RenameTemplate(LsNewName)
    End Sub
#End Region
#Region "Functions"
    'initialization of controls
    Public Function Init(ByVal IsPathToDiagram As String) As GeActionResult
        'setting path
        MsPathToDiagram = IsPathToDiagram
        'refreshing
        RefreshToolbar
    End Function
    'refreshing toolbar with data from XML file
    Public Function RefreshToolbar() As GeActionResult
        Try
            'clearing
            Clear()
            'loading and filling imagelistbox
            fcDiagram.LoadFromXml(MsPathToDiagram)
            For Each LoMMTableNode As ClsMMTableNode In fcDiagram.Nodes
                ilbNodes.Items.Add(LoMMTableNode.Caption, 0)
            Next
            Return GeActionResult.kkOk
        Catch ex As Exception
            Debug.Print("CHYBA!!! - UcnNodesToolbar.Init")
            Return GeActionResult.kkError
        End Try
    End Function
    'saving toolbar to XML file after adding template
    Public Function SaveToolbar(Optional ByVal IsPathToDiagram As String = "") As GeActionResult
        Try
            Dim LsPathToDiagram As String = IIf(IsPathToDiagram <> "", IsPathToDiagram, MsPathToDiagram)
            fcDiagram.SaveToXml(LsPathToDiagram)
            Return GeActionResult.kkOk
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
    'remove template from toolbar
    Public Function RemoveTemplateFromToolbar() As GeActionResult
        Try
            Dim LoMMTableNode As ClsMMTableNode = GetTemplate(ilbNodes.SelectedValue)
            If LoMMTableNode IsNot Nothing Then
                fcDiagram.Nodes.Remove(LoMMTableNode)
            Else
                Return GeActionResult.kkError
            End If

            'saving changes
            SaveToolbar()
            RefreshToolbar()
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
    'gets name of template
    Public Function GetNameOfTemplate() As String
        Return ilbNodes.SelectedValue
    End Function
    'gets node-template with specific name
    Public Function GetTemplate(ByVal IsTemplateName As String) As ClsMMTableNode
        Try
            For Each LoMMTableNode As ClsMMTableNode In fcDiagram.Nodes
                If LoMMTableNode.Caption = IsTemplateName Then Return LoMMTableNode
            Next
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    'renaming template
    Private Function RenameTemplate(ByVal IsNewName As String) As GeActionResult
        Try
            If IsNewName <> "" Then
                '!!!!!!!!!!!!!!!!!!!!!
                Dim LoMMTableNode As ClsMMTableNode = GetTemplate(GetNameOfTemplate)
                LoMMTableNode.Caption = IsNewName
                'after this, table has new name but data in its datatable are incorrect because there is different name of table.
                'this situation is solved when template is added to diagram
                '!!!!!!!!!!!!!!!!!!!!!

                SaveToolbar()
                RefreshToolbar()
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - RenameTemplate - Prazdny nazov")
                RefreshToolbar()
                Return GeActionResult.kkOk
            End If
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Subs"
    Private Sub Clear()
        ilbNodes.Items.Clear()
        fcDiagram.ClearAll()
    End Sub
#End Region

End Class
